package aiku.numericalcontroloa.Service.CompanyCulture.Impl;

import aiku.numericalcontroloa.Base.Utils.BaseEnum;
import aiku.numericalcontroloa.Base.Utils.HttpStatus;
import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.UserMapper;
import aiku.numericalcontroloa.Mapper.CompanyCulture.CommentMapper;
import aiku.numericalcontroloa.Model.Dto.CompanyCulture.CommentDto;
import aiku.numericalcontroloa.Model.Entity.User;
import aiku.numericalcontroloa.Model.Entity.CompanyCulture.Comment;
import aiku.numericalcontroloa.Model.Vo.CompanyCulture.CommentVo;
import aiku.numericalcontroloa.Service.CompanyCulture.CommentService;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author 黄广庭
 * @Date 2024/1/10 16:54
 */
@Service
public class CommentServiceImpl implements CommentService {
    private final CommentMapper commentMapper;
    private final UserMapper userMapper;

    public CommentServiceImpl(CommentMapper commentMapper, UserMapper userMapper) {
        this.commentMapper = commentMapper;
        this.userMapper = userMapper;
    }

    @Override
    public Result addComment(CommentDto commentDto) {
        if (commentDto == null || commentDto.getContent().isEmpty())
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto,comment);
        comment.setUserId(StpUtil.getLoginId().toString());
        commentMapper.insert(comment);
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        // 用户名
        User user = userMapper.selectById(comment.getUserId());
        commentVo.setUserName(user.getNickName());
        commentVo.setImage(user.getImage());
        // 处理日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        commentVo.setCreateTime(sdf.format(comment.getCreateTime()));
        return Result.success("评论成功",commentVo);
    }

    @Override
    public Result deleteUserComment(CommentDto commentDto) {
        if (commentDto == null || commentDto.getId() == null)
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        commentMapper.deleteById(commentDto.getId());
        return Result.success("删除成功");
    }

    @Override
    public Result deleteComment(String materialId) {
        if (materialId == null || materialId.isEmpty()){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(Comment::getMaterialId,materialId);
        int delete = commentMapper.delete(commentWrapper);
        if (delete > 0)
            return Result.success("删除成功");
        return Result.error("删除失败");
    }

    @Override
    public Result getComment(CommentDto commentDto) {
        if (commentDto == null || commentDto.getMaterialId() == null
                && commentDto.getCurrentPage() == null)
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        Page<Comment> page = new Page<>(commentDto.getCurrentPage(),commentDto.getPageSize());
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.orderByAsc(Comment::getCreateTime)
                .eq(Comment::getMaterialId,commentDto.getMaterialId());
        List<Comment> commentList = commentMapper.selectPage(page, commentWrapper).getRecords();
        List<CommentVo> commentVoList = new ArrayList<>();
        if (commentList.isEmpty()){
            return Result.success("查询成功",commentVoList);
        }
        Set<String> userIdList = commentList.stream().map(Comment::getUserId).collect(Collectors.toSet());
        List<User> userList = userMapper.selectBatchByIdWithDelete(userIdList);
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        for (Comment comment : commentList){
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(comment,commentVo);
            // 查询用户名
            if (userMap.containsKey(comment.getUserId())){
                User user = userMap.get(comment.getUserId());
                commentVo.setUserName(user.getNickName());
                commentVo.setImage(user.getImage());
            }
            // 处理日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            commentVo.setCreateTime(sdf.format(comment.getCreateTime()));
            commentVoList.add(commentVo);
        }
        return Result.success("查询成功",commentVoList);
    }


}
